#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"

//create node of project
proj_node* createprojnode(const char* str,const char* ip,const char* port){
    proj_node* newNode=(proj_node*)malloc(sizeof(proj_node));
    if(!newNode){
        printf("Memory allocation error\n");
	exit(1);
    }
    strncpy(newNode->str,str,sizeof(newNode->str)-1);
    newNode->str[sizeof(newNode->str)-1]='\0';
    strncpy(newNode->ip,ip,sizeof(newNode->ip)-1);
    newNode->ip[sizeof(newNode->ip)-1]='\0';
    strncpy(newNode->port,port,sizeof(newNode->port)-1);
    newNode->port[sizeof(newNode->port)-1]='\0';
    newNode->status=6;
    newNode->next=NULL;
    return newNode;    
}

void appendprojnode(proj_node** head,const char* str,const char* ip,const char* port){
    proj_node* newNode=createprojnode(str,ip,port);
    if(*head==NULL){
        *head=newNode;
    }else{
        proj_node* temp=*head;
	while(temp->next!=NULL){
            temp=temp->next;
	}
	temp->next=newNode;
    }
}
void deleteprojnode(proj_node** head,const char* str){
    proj_node* temp=*head;
    proj_node* prev=NULL;
    if(temp != NULL && strcmp(temp->str,str)==0){
        *head=temp->next;
	free(temp);
	return;
    }
    while(temp != NULL && strcmp(temp->str,str)!=0){
        prev=temp;
	temp=temp->next;
    }
    if(temp==NULL){
        printf("project %s not found.\n",str);
        return;
    }
    prev->next=temp->next;
    free(temp);
}
